package com.online.questionnaire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.online.questionnaire.QuestionType;
import com.online.questionnaire.common.ErrorCode;
import com.online.questionnaire.exception.BusinessException;
import com.online.questionnaire.mapper.OptionsMapper;
import com.online.questionnaire.model.Options;
import com.online.questionnaire.model.dto.QuestionsDTO;
import com.online.questionnaire.service.QuestionsService;
import com.online.questionnaire.model.Questions;
import com.online.questionnaire.mapper.QuestionsMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
* @author 27701
* @description 针对表【questions】的数据库操作Service实现
* @createDate 2025-02-13 16:30:47
*/
@Service
public class QuestionsServiceImpl extends ServiceImpl<QuestionsMapper, Questions>
    implements QuestionsService {

    @Resource
    OptionsMapper optionsMapper;

    @Override
    public QuestionsDTO getDTOById(Long id) {
        Questions byId = this.getById(id);
        if (byId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"题目不存在");
        }
        QuestionsDTO questionsDTO = new QuestionsDTO();
        BeanUtils.copyProperties(byId,questionsDTO);

        LambdaQueryWrapper<Options> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Options::getQuestionid, id);
        List<Options> options = optionsMapper.selectList(queryWrapper);
        questionsDTO.setOptions(options);
        return questionsDTO;

    }

    @Override
    public boolean saveQuestions(QuestionsDTO questions, Long surveyId, Long userId) {

        //新增
        if (questions.getId() == null){
          return addQuestions(questions,surveyId, userId);
        }

        questions.setSurveyid(surveyId);
        this.save(questions);

        //判断是否有下属选项 单选/多选
        if (QuestionType.SINGLE_CHOICE.getCode() == questions.getType()
                || QuestionType.MULTIPLE_CHOICE.getCode() == questions.getType()){
            //清空选项 重新保存
            QueryWrapper<Options> delWrapper = new QueryWrapper<>();
            delWrapper.eq("questionId", questions.getId());
            optionsMapper.delete(delWrapper);

            List<Options> optionsList = questions.getOptions();
            for (int i = 0; i < optionsList.size(); i++) {
                Options option = optionsList.get(i);
                if (StringUtils.isBlank(option.getValue())) {
                    throw new RuntimeException("选项值不能为空");
                }
                // 检查 value 值是否重复
                for (int j = 0; j < optionsList.size(); j++) {
                    if (i == j){
                        continue;
                    }
                    if (option.getValue().equals(optionsList.get(j).getValue())){
                        throw new RuntimeException("选项值重复");
                    }
                    //检查orders是否重复
                    if (Objects.equals(option.getOrders(), optionsList.get(j).getOrders())){
                        throw new RuntimeException("选项排序重复");
                    }
                }

                option.setQuestionid(questions.getId());
                optionsMapper.insert(option);
            }
        }

        return true;

    }

    @Override
    public boolean addQuestions(QuestionsDTO questions, Long surveyId, Long userId) {

        questions.setSurveyid(surveyId);
        //先保存题目
        boolean save = this.save(questions);
        if (!save){
            throw new RuntimeException("题目保存失败");
        }
        Long questionId = questions.getId();
        //判断是否有下属选项 单选/多选
        if (QuestionType.SINGLE_CHOICE.getCode() == questions.getType()
                || QuestionType.MULTIPLE_CHOICE.getCode() == questions.getType()){
            List<Options> optionsList = questions.getOptions();
            for (int i = 0; i < optionsList.size(); i++) {
                Options option = optionsList.get(i);
                option.setId(null);
                if (StringUtils.isBlank(option.getValue())) {
                    throw new RuntimeException("选项值不能为空");
                }
                // 检查 value 值是否重复
                for (int j = 0; j < optionsList.size(); j++) {
                    if (i == j){
                        continue;
                    }
                    if (option.getValue().equals(optionsList.get(j).getValue())){
                        throw new BusinessException(ErrorCode.PARAMS_ERROR,"选项值重复");
                    }
                    //检查orders是否重复
                    if (Objects.equals(option.getOrders(), optionsList.get(j).getOrders())){
                        throw new BusinessException(ErrorCode.PARAMS_ERROR,"选项排序重复");
                    }
                }

                option.setQuestionid(questionId);
                optionsMapper.insert(option);
            }
        }

        return true;

    }
}




